// pages/init-navigation/index.js
import {
  routeAndState
} from '../../dataSource'
import {
  _get,
  update
} from '../../api/collection_record'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: routeAndState,
    result: [],
    allData: [],
    dataSource: {},
    id: ''
  },

  onChange(event) {
    this.setData({
      result: event.detail,
    });
  },
  setPageProperty(options) {
    wx.setNavigationBarTitle({
      title: '设计采集流程'
    })
    this.setData({
      id: options.id
    })
  },
  toggle(event) {
    const {
      index
    } = event.currentTarget.dataset;
    const checkbox = this.selectComponent(`.checkboxes-${index}`);
    checkbox.toggle();
  },

  noop() {},
  setDataSource() {
    this.setData({
      result: routeAndState.map(it => it.key),
      allData: routeAndState.map(it => it.key)
    })
  },
  next() {
    const {
      result,
      dataSource,
      allData
    } = this.data;
    for (let x of allData) {
      if (result.includes(x)===false) {
          dataSource[x] = -1;
      }
    }
    update(dataSource,()=>{
      wx.redirectTo({
        url: '../navigation/index?id=' + this.data.id,
      });
    })
  },
  fetchData() {
    _get(this.data.id || 5, res => {
      this.setData({
        dataSource: res
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setPageProperty(options);
    this.fetchData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.setDataSource();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})